package com.voicechat.firstmet.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.voicechat.firstmet.R;
import com.voicechat.firstmet.activity.PersonalHomepageActivity;
import com.voicechat.firstmet.adapter.AttachmentPersonAdapter;
import com.voicechat.firstmet.base.BaseFragment;
import com.voicechat.firstmet.bean.FristMetBean;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.service.FaintService;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.fragment_shar_girls
 * @ClassName: ShareGirlsFragment
 * @Description: 隐约    ------附件的人
 * @Author: ljp
 * @CreateDate: 2019/1/4 19:44
 * @Version: 1.0
 */
public class AttachmentPersonFragment extends BaseFragment {
    @BindView(R.id.recycler_list)
    RecyclerView recyclerList;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smartRefresh;

    private int page = 1;
    private MySelfInfoBean.ObjectBean mySeleInfo;
    private String token;
    private String deviceInfo;
    private String TAG = "AttachmentPersonFragment";
    private List<FristMetBean.PageBean.ListBean> list;
    private AttachmentPersonAdapter attachmentPersonAdapter;

    @Override
    protected int loadLayout() {
        return R.layout.fragment_attachment;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this, view);
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        token = SPUtils.getEditoString(SPUtils.SP_TOKEN);
        deviceInfo = PhoneUtils.getDeviceInfo(context);
        initView();
    }

    public static AttachmentPersonFragment newInstance() {
        AttachmentPersonFragment attachmentPersonFragment = new AttachmentPersonFragment();
        return attachmentPersonFragment;
    }

    /**
     * @return
     * @method
     * @description 数据控件实例化
     * @date: 2019/1/4 20:00
     * @author: ljp
     */
    private void initView() {
        list = new ArrayList<>();
        smartRefresh.autoRefresh();
        //下拉刷新
        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                initHttp();
                smartRefresh.finishRefresh(10000);
            }
        });
        //上拉加载更多
        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                initHttp();
                smartRefresh.finishLoadMore(10000);
            }
        });
        //设置 Header 为 贝塞尔雷达 样式
        smartRefresh.setRefreshHeader(new ClassicsHeader(context));
        smartRefresh.setRefreshFooter(new ClassicsFooter(context));
        attachmentPersonAdapter = new AttachmentPersonAdapter(context, list, R.layout.item_attachment_person);
        recyclerList.setLayoutManager(new LinearLayoutManager(context));
        recyclerList.setAdapter(attachmentPersonAdapter);
        attachmentPersonAdapter.setOnClickItem(new AttachmentPersonAdapter.OnClickItem() {
            @Override
            public void setOnClickItem(int position, FristMetBean.PageBean.ListBean listBean) {
                Intent intent = new Intent(context, PersonalHomepageActivity.class);
                intent.putExtra("anchorId", listBean.getId());
                context.startActivity(intent);
            }
        });
        initHttp();
    }

    private void initHttp() {
        String longitude = "";
        String dimension = "";
        if(!TextUtils.isEmpty(SPUtils.getEditoString("longitude"))){
            longitude = SPUtils.getEditoString("longitude");
        }
        if(!TextUtils.isEmpty(SPUtils.getEditoString("dimension"))){
            dimension = SPUtils.getEditoString("dimension");
        }
        FaintService.attachmentPerson(token, deviceInfo, mySeleInfo.getId(), longitude, dimension,page).subscribe(fristMetBean -> {
            if (fristMetBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                int i = fristMetBean.getPage().getCount() / fristMetBean.getPage().getPageSize() +
                        (fristMetBean.getPage().getCount() % fristMetBean.getPage().getPageSize() == 0 ? 0 : 1);
                if (i != 0 && page > i) {//判断是否是最后一页
                    smartRefresh.finishLoadMoreWithNoMoreData();//完成加载并标记没有更多数据 1.0.4
                } else {
                    if (page == 1) {
                        list.clear();
                        smartRefresh.finishRefresh();
                    }
                    if (fristMetBean.getPage().getList() != null) {
                        list.addAll(fristMetBean.getPage().getList());
                        attachmentPersonAdapter.notifyDataSetChanged();
                    } else {
                    }
                    smartRefresh.finishLoadMore();
                }
            } else if (fristMetBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            } else {
                toast_MakeText(fristMetBean.getMsg());
            }
        }, throwable -> {
            Log.i(TAG, "initHttp: --------------"+throwable);
            toast_MakeText(getResources().getString(R.string.net_error));
        });

    }
}
